package com.dji.ux.sample.CustomView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;

import com.dji.ux.sample.MECSignalClient.Vis_data;
import com.google.gson.Gson;

import org.json.JSONObject;

public class AISurfaceViewManager {
    private Context mcontext;
    private SurfaceHolder mAISurfaceHolder;

    private int screenWidth = 0;
    private int screenHeight = 0;

    private String TAG = "AISurfaceViewManager";
    private Paint mPaint;

    private Canvas mCanvas;

    public AISurfaceViewManager(Context context){
        mcontext = context;
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3f);
    }

    public void setAISurfaceViewHolder(SurfaceHolder surfaceHolder){
        mAISurfaceHolder = surfaceHolder;
    }

    public void clear(){
        try {
            mCanvas = mAISurfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //提交画板
            mAISurfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }
    //社交距离识别可视化
    public void draw(JSONObject dataSource){
        Log.i(TAG, "onMessage: "+dataSource);

        try {
            //初始化画板
            mCanvas = mAISurfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

            //提取矩形识别框坐标
            String strRectList = dataSource.getString("rect_list") .replace("[","").replace("]","").replace("\"", "").replace(" ","");
            Log.i(TAG, "onMessage: strRectList = " + strRectList);

            //绘制社交距离识别框坐标
            String[] RectList = strRectList.split(",");
            for (int RectList_i = 0; RectList_i < RectList.length; RectList_i++){
                if (0 == (RectList_i + 1) % 5){
                    int rectType = Integer.parseInt(RectList[RectList_i]);
                    //根据不同类型选择绘制颜色
                    switch (rectType){
                        case 0:
                            mPaint.setColor(Color.GREEN);
                            break;
                        case 2:
                            mPaint.setColor(Color.YELLOW);
                            break;
                        case 1:
                            mPaint.setColor(Color.RED);
                            break;
                    }
                    //像素坐标转换
                    int left = weightToPixel_W(RectList[RectList_i - 4]);
                    int top = weightToPixel_H(RectList[RectList_i - 3]);
                    int right = weightToPixel_W(RectList[RectList_i - 2]);
                    int bottom = weightToPixel_H(RectList[RectList_i - 1]);
                    Log.i(TAG, "drawRect  left: , top: , right: , bottom: , rectType: " + left + "---" + rectType + "---" + left  + "---" + top + "---" + right + "---" + bottom);
                    mCanvas.drawRect(left, top, right, bottom, mPaint);
                }
            }

            //提取连线坐标
            String strLineList = dataSource.getString("line_list").replace("[","").replace("]","").replace("\"", "").replace(" ","");
            Log.i(TAG, "onMessage: strLineList = " + strLineList);

            //绘制识别框连线坐标
            String[] LineList = strLineList.split(",");
            for (int LineList_i = 0; LineList_i < LineList.length; LineList_i++){
                if (0 == (LineList_i + 1) % 5){
                    int lineType = Integer.parseInt(LineList[LineList_i]);
                    Log.i(TAG, "drawLine: line_type: " + lineType);
                    //根据不同类型选择绘制颜色
                    switch (lineType){
                        case 1:
                            mPaint.setColor(Color.YELLOW);
                            break;
                        case 0:
                            mPaint.setColor(Color.RED);
                            break;
                    }
                    //像素坐标转换
                    int start_x = weightToPixel_W(LineList[LineList_i - 4]);
                    int start_y = weightToPixel_H(LineList[LineList_i - 3]);
                    Log.i(TAG, "drawLine: start_x: , start_y: " + "---" + start_x + "---" + start_y);
                    int end_x = weightToPixel_W(LineList[LineList_i - 2]);
                    int end_y = weightToPixel_H(LineList[LineList_i - 1]);
                    Log.i(TAG, "drawLine: end_x: , end_y: " + "---" + end_x + "---" + end_y);
                    mCanvas.drawLine(start_x, start_y, end_x, end_y, mPaint);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //提交画板
            mAISurfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }
    //二次定位识别可视化
    public void drawCV(JSONObject dataSource){
        Log.i(TAG, "onMessage: "+dataSource);

        try {
            //初始化画板

            mCanvas = mAISurfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

            //提取矩形识别框坐标
            String strRectList = dataSource.getString("CVrect_list") .replace("[","").replace("]","").replace("\"", "").replace(" ","");
            Log.i(TAG, "onMessage: strCVRectList = " + strRectList);

            //绘制社交距离识别框坐标
            String[] RectList = strRectList.split(",");
            for (int RectList_i = 0; RectList_i < RectList.length; RectList_i++){
                if (0 == (RectList_i + 1) % 5){
                    int rectType = Integer.parseInt(RectList[RectList_i]);
                    //根据不同类型选择绘制颜色
                    switch (rectType){
                        case 0:
                            mPaint.setColor(Color.GREEN);
                            break;
                        case 2:
                            mPaint.setColor(Color.YELLOW);
                            break;
                        case 1:
                            mPaint.setColor(Color.RED);
                            break;
                    }
                    //像素坐标转换
                    int left = weightToPixel_W(RectList[RectList_i - 4]);
                    int top = weightToPixel_H(RectList[RectList_i - 3]);
                    int right = weightToPixel_W(RectList[RectList_i - 2]);
                    int bottom = weightToPixel_H(RectList[RectList_i - 1]);
                    Log.i(TAG, "drawRect  left: , top: , right: , bottom: , rectType: " + left + "---" + rectType + "---" + left  + "---" + top + "---" + right + "---" + bottom);
                    mCanvas.drawRect(left, top, right, bottom, mPaint);
                }
            }

            //提取连线坐标
            String strLineList = dataSource.getString("CVline_list").replace("[","").replace("]","").replace("\"", "").replace(" ","");
            Log.i(TAG, "onMessage: strLineList = " + strLineList);

            //绘制识别框连线坐标
            String[] LineList = strLineList.split(",");
            for (int LineList_i = 0; LineList_i < LineList.length; LineList_i++){
                if (0 == (LineList_i + 1) % 5){
                    int lineType = Integer.parseInt(LineList[LineList_i]);
                    Log.i(TAG, "drawLine: line_type: " + lineType);
                    //根据不同类型选择绘制颜色
                    switch (lineType){
                        case 1:
                            mPaint.setColor(Color.YELLOW);
                            break;
                        case 0:
                            mPaint.setColor(Color.RED);
                            break;
                    }
                    //像素坐标转换
                    int start_x = weightToPixel_W(LineList[LineList_i - 4]);
                    int start_y = weightToPixel_H(LineList[LineList_i - 3]);
                    Log.i(TAG, "drawLine: start_x: , start_y: " + "---" + start_x + "---" + start_y);
                    int end_x = weightToPixel_W(LineList[LineList_i - 2]);
                    int end_y = weightToPixel_H(LineList[LineList_i - 1]);
                    Log.i(TAG, "drawLine: end_x: , end_y: " + "---" + end_x + "---" + end_y);
                    mCanvas.drawLine(start_x, start_y, end_x, end_y, mPaint);
                }
            }
            // 绘制屏幕中央十字
            mPaint.setColor(Color.BLUE);
            mCanvas.drawLine(screenWidth * 0.4f, screenHeight * 0.5f, screenWidth * 0.6f, screenHeight * 0.5f, mPaint);
            mCanvas.drawLine(screenWidth * 0.5f, screenHeight * 0.3f, screenWidth * 0.5f, screenHeight * 0.7f, mPaint);

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //提交画板
            mAISurfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }


    //像素权重转换为手机像素的横坐标
    public int weightToPixel_W(String weight){
        return (int) Math.round(screenWidth * Float.parseFloat(weight));
    }

    //像素权重转换为手机像素的纵坐标
    public int weightToPixel_H(String weight){
        return (int) Math.round(screenHeight * Float.parseFloat(weight));
    }
}
